package com.threeman.mapswaypoints1;

import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class WaypointActivity extends Activity {

	private Context mContext;
	private int mRequestCode;
	private GeoPoint mPoint;
	private String mAdress;
	private String mName;
	private String mPoi;
	private String mDescription;
	
	private EditText mNameEditText;
	private EditText mDescriptionEditText;
	private TextView mLocalizationTextView;
	private TextView mAdressTextView;
	private Spinner mSpinner;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //View
        setContentView(R.layout.waypoint);
        mContext = getApplicationContext();
        
        mNameEditText = (EditText) findViewById(R.id.editTextWaypointName);
        mDescriptionEditText = (EditText) findViewById(R.id.editTextDescription);
        mLocalizationTextView = (TextView) findViewById(R.id.textViewLocalization);
        mAdressTextView = (TextView) findViewById(R.id.textViewAdress);
        mSpinner = (Spinner) findViewById(R.id.spinnerPOI);
        
        // Populate the update frequency spinner
        ArrayAdapter<CharSequence> poiAdapter;
        poiAdapter = ArrayAdapter.createFromResource(this, R.array.POIarray, android.R.layout.simple_spinner_item);
        int spinner_dropdown_item = android.R.layout.simple_spinner_dropdown_item;
        poiAdapter.setDropDownViewResource(spinner_dropdown_item);
        mSpinner.setAdapter(poiAdapter);
        mSpinner.setOnItemSelectedListener(new mSpinnerOnItemSelectedListener());
        
        setResult(100);
        
		try
		{
	        //get params
	        Bundle bun = this.getIntent().getExtras();
	        
	        mRequestCode = bun.getInt("requestCode");
      
	        int lon = bun.getInt("lon");
	        int lat = bun.getInt("lat");
	        
	        mPoint = new GeoPoint(lat, lon);
	        
	        mName = bun.getString("name");
	        mAdress = bun.getString("adress");
	        
	        if(mRequestCode == 12)
	        {
	        	mDescription = bun.getString("description");
	        	mPoi = bun.getString("poi");
	        	
		        //spiner
		        ArrayAdapter<String> myAdap = (ArrayAdapter) mSpinner.getAdapter(); //cast to an ArrayAdapter
		        
		        int spinnerPosition = myAdap.getPosition(mPoi);
		        //set the default according to value
		        mSpinner.setSelection(spinnerPosition);
	        }
	        else
	        {
	        	mDescription = "opis";
	        }
	        
	        mNameEditText.setText(mName);
	        mAdressTextView.setText(mAdress);
	        mDescriptionEditText.setText(mDescription);
	        
	        
	        String localization = (mPoint.getLatitudeE6()  / 1E6) + " N, " + (mPoint.getLongitudeE6() / 1E6 + " E");
	       
	        mLocalizationTextView.setText(localization);
		}
		catch(Exception ex)
		{
			showToastMessage(ex.toString());
		}
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            
        }
        return super.onKeyDown(keyCode, event);
    }
    
    public void addClick(View v) 
    {
		try
		{
			//showToastMessage("DODANO MARKER");
			
	    	Bundle bundle = new Bundle();

	    	bundle.putInt("lon", mPoint.getLongitudeE6());
	    	bundle.putInt("lat", mPoint.getLatitudeE6());
	    	bundle.putString("name", mNameEditText.getText().toString());
	    	bundle.putString("adress", mAdress);
	    	bundle.putString("description", mDescriptionEditText.getText().toString());
	    	bundle.putString("poi", mPoi);
	    	//showToastMessage(mPoi);
	    	
	    	Intent i = new Intent();

	    	i.putExtras(bundle);

	    	setResult(11, i);
	    	
	    	finish();
		}
		catch(Exception ex)
		{
			showToastMessage("addClick\n" + ex.toString());
		}
    }
    
    /*
     * OTHER METHODS
     */
	private void showToastMessage(String msg)
	{

	      Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);

	      toast.show();

	}
	
	/*
	 * 
	 * ONSELECTEDITEM SPINNER LISTENER
	 * 
	 */
	
	public class mSpinnerOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
			// TODO Auto-generated method stub
			mPoi = parent.getItemAtPosition(pos).toString();
		}

		public void onNothingSelected(AdapterView parent) {
			// TODO Auto-generated method stub
			mPoi = "Inne";
		}
	}
	
}
